<?php

namespace app\api\controller;

use app\model\goods\GoodsCategory as GoodsCategoryModel;
use app\model\member\Member as MemberModel;
/**
 * 商品分类
 * Class Goodscategory
 * @package app\api\controller
 */
class Goodscategory extends BaseApi
{

    /**
     * 树状结构信息
     */
    public function tree()
    {
        $level = isset($this->params[ 'level' ]) ? $this->params[ 'level' ] : 3;// 分类等级 1 2 3
        $latitude = isset($this->params[ 'latitude' ]) ? $this->params[ 'latitude' ] : 0;
        $longitude = isset($this->params[ 'longitude' ]) ? $this->params[ 'longitude' ] : 0;
        $type = isset($this->params[ 'type' ]) ? $this->params[ 'type' ] : 0;
        $goods_category_model = new GoodsCategoryModel();
        $condition = [
            [ 'is_show', '=', 0 ],
            [ 'level', '<=', $level ],
            [ 'site_id', '=', $this->site_id ]
        ];
        
        $token = $this->checkToken();
        
        
        
       
        
        $field = "category_id,category_name,short_name,pid,level,image,category_id_1,category_id_2,category_id_3,image_adv,link_url,longitude,latitude";
        $order = "sort asc,category_id desc";
        
        if($type=='edit'){
            $list = $goods_category_model->getCategoryTreeMain($condition, $field, $order);
        
            $result = [];
            foreach ($list['data'] as $v){
                $result[] = $v;
            }
             $list['data'] = $result;

            return $this->response($list);
            
        }
        
        //已登录
        if(isset( $token[ 'data' ][ 'member_id' ]) && empty($tpye) ){
            $member_model = new MemberModel();
            $info = $member_model->getMemberInfo([ [ 'member_id', '=', $token[ 'data' ][ 'member_id' ], [ 'site_id', '=', $this->site_id ] ] ], 'member_id,source_member,username,nickname,mobile,email,password,status,headimg,member_level,member_level_name,member_label,member_label_name,qq,qq_openid,wx_openid,wx_unionid,ali_openid,baidu_openid,toutiao_openid,douyin_openid,realname,sex,location,birthday,point,balance,balance_money,growth,sign_days_series,password,member_level_type,level_expire_time,is_edit_username,is_fenxiao,province_id,city_id,district_id,community_id,address,full_address,longitude,latitude');
            $condition[] = ['pid','=',$info['data']['location']];
            $condition[] = ['level', '>', 1];
            $list = $goods_category_model->getCategoryTreeNew($condition, $field, $order);
        }else{
            $condition[] = ['pid','=',1];
            $condition[] = ['level', '>', 1];
            $list = $goods_category_model->getCategoryTreeNew($condition, $field, $order);
        }
        
        
        
        $result = [];
        foreach ($list['data'] as $v){
            //if(!empty($longitude) && !empty($latitude)){
                // $d = getDistance($longitude,$latitude,  $v['longitude'], $v['latitude']);
            
                // if($d<2000){
                    $result[] = $v;
                //}
            //}
            
        }
        $list['data'] = $result;

        return $this->response($list);
    }
    
    /**
     * 树状结构信息
     */
    public function treeMain()
    {
        $level = isset($this->params[ 'level' ]) ? $this->params[ 'level' ] : 3;// 分类等级 1 2 3
        $latitude = isset($this->params[ 'latitude' ]) ? $this->params[ 'latitude' ] : 0;
        $longitude = isset($this->params[ 'longitude' ]) ? $this->params[ 'longitude' ] : 0;
        $goods_category_model = new GoodsCategoryModel();
        $condition = [
            [ 'is_show', '<=', 0 ],
            [ 'level', '=', $level ],
            [ 'site_id', '=', $this->site_id ]
        ];
        
        $field = "category_id,category_name,short_name,pid,level,image,category_id_1,category_id_2,category_id_3,image_adv,link_url,longitude,latitude";
        $order = "sort asc,category_id desc";
        $list = $goods_category_model->getCategoryTreeMain($condition, $field, $order);
        
        $result = [];
        foreach ($list['data'] as $v){
            //if(!empty($longitude) && !empty($latitude)){
                // $d = getDistance($longitude,$latitude,  $v['longitude'], $v['latitude']);
            
                // if($d<2000){
                    $result[] = $v;
                //}
            //}
            
        }
        $list['data'] = $result;

        return $this->response($list);
    }
    
    /**
     * 树状结构信息
     */
    public function treeNav()
    {
        $level = isset($this->params[ 'level' ]) ? $this->params[ 'level' ] : 3;// 分类等级 1 2 3
        $latitude = isset($this->params[ 'latitude' ]) ? $this->params[ 'latitude' ] : 0;
        $longitude = isset($this->params[ 'longitude' ]) ? $this->params[ 'longitude' ] : 0;
        $goods_category_model = new GoodsCategoryModel();
        $condition = [
            [ 'is_show', '=', 0 ],
            [ 'level', '=', $level ],
            [ 'site_id', '=', $this->site_id ]
        ];
        
        $field = "category_id,category_name,short_name,pid,level,image,category_id_1,category_id_2,category_id_3,image_adv,link_url,longitude,latitude";
        $order = "sort asc,category_id desc";
        $list = $goods_category_model->getCategoryTreeNav($condition, $field, $order);
        
        $result = [];
        foreach ($list['data'] as $v){
            //if(!empty($longitude) && !empty($latitude)){
                // $d = getDistance($longitude,$latitude,  $v['longitude'], $v['latitude']);
            
                // if($d<2000){
                    $result[] = $v;
                //}
            //}
            
        }
        $list['data'] = $result;

        return $this->response($list);
    }

    public function info()
    {
        $category_id = $this->params['category_id'] ?? 0;
        $goods_category_model = new GoodsCategoryModel();
        $data = $goods_category_model->getCategoryInfo([ ['category_id', '=', $category_id], ['site_id', '=', $this->site_id] ]);
        if (!empty($data['data'])) {
            $child_list = $goods_category_model->getCategoryList([ ['pid', '=', $category_id], ['site_id', '=', $this->site_id], ['is_show', '=', 0] ], 'category_id,category_name,short_name,pid,level,is_show,sort,image,attr_class_id,attr_class_name,category_id_1,category_id_2,category_id_3,commission_rate,image_adv', 'sort asc,category_id desc');
            $data['data']['child_list'] = $child_list['data'];
        }
        return $this->response($data);
    }
}